﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using DoxygenBrowserAddin.Views;
using DoxygenBrowser.Common.Model;
using EnvDTE;
using System.Windows.Forms;
using DoxygenBrowserAddin.Data;

namespace DoxygenBrowserAddin.Controller
{
  public class CmdDoxygenConfig : AddInCommand
  {
    public CmdDoxygenConfig(string strShortName,
                            DoxAddinController controller)
      : base(strShortName, controller)
    {
      _strButtonText = "Configure Doxygen";
      _strToolTipText = "Executes the command for DoxygenAddin";
      _iIconID = 59;
    }

    #region overrides
    public override void QueryStatus(EnvDTE.vsCommandStatusTextWanted neededText, ref EnvDTE.vsCommandStatus status, ref object commandText)
    {
      base.QueryStatus(neededText, ref status, ref commandText);

      Solution solution = _controller.Application.Solution;
      status = (vsCommandStatus)(status  & ~vsCommandStatus.vsCommandStatusEnabled);
      if((solution != null) && (solution.IsOpen))
      {
        status = (vsCommandStatus)(status | vsCommandStatus.vsCommandStatusEnabled);
      }
      
    }

    public override bool Execute(EnvDTE.vsCommandExecOption executeOption, ref object varIn, ref object varOut)
    {

      DoxygenConfig doxygenConfig = _controller.DoxygenConfig;

      if (doxygenConfig == null)
      {
        doxygenConfig = checkCreateDoxygenConfig();
      }

      if (doxygenConfig != null)
      {
        FrmDoxygenConfig dlg = new FrmDoxygenConfig(_controller.Application.Solution.FullName);
        if (dlg.ShowDialog(_controller.DoxygenConfig) == DialogResult.OK)
        {
          _controller.DoxygenConfig.Save(_controller.DoxygenConfigPath);
          if (dlg.DoxPathChanged)
          {            
            _controller.CreateTokenDictionary();
          }
          _controller.OnDoxygenConfigChanged();
        }
      }
      return true;
    }

    private DoxygenConfig checkCreateDoxygenConfig()
    {
      DoxygenConfig doxygenConfig = null;
      DialogResult ret = MessageBox.Show("Do you want to add Doxygen Configuration to this solution?",
                                         "Doxygen Configuration",
                                         MessageBoxButtons.YesNo);
      if(ret == DialogResult.Yes)
      {
        string strDirectoryName = Path.GetDirectoryName(_controller.Application.Solution.FullName);
        string strSolutionName = Path.GetFileNameWithoutExtension(_controller.Application.Solution.FileName);

        string strDoxConfigPath = Path.Combine(strDirectoryName, strSolutionName +
                                                                 DoxAddinController.DOXDOC_EXTENSION);

        doxygenConfig = _controller.CreateDoxConfig(strDoxConfigPath);
      }
      return doxygenConfig;
    }

    #endregion overrides


  }
}
